﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using System.Data;
using Ext.Net;

namespace TCDC_System.Content.TransectionData
{
    public partial class user_manage : System.Web.UI.Page
    {
  
        private UserManage objUserManage;
        private tcdc_mst_usertype objUserType;
        private UserManageRoleDetail objUserRoleDetail;

        protected void Page_Init(object sender, EventArgs e)
        {
            this.UserManageAdd1.GridStore = StoreUser;
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            objUserManage = new UserManage();
            objUserManage.strConn = ConfigurationManager.AppSettings["ConnString"];

            if (!X.IsAjaxRequest)
            {
                StoreUser.DataSource = GetUser();
                StoreUser.DataBind();

                StoreUserType.DataSource = GetUserType();
                StoreUserType.DataBind();
            }
        }

        protected DataTable GetUser()
        {
            DataTable dt = objUserManage.getByObject(new UserManage());
            return dt;
        }

        protected DataTable GetUserType()
        {
            objUserType = new tcdc_mst_usertype();
            objUserType.strConn = ConfigurationManager.AppSettings["ConnString"];
            DataTable dt = objUserType.getByObject(new tcdc_mst_usertype());
            return dt;
        }

        protected void StoreUser_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }

        protected void StoreUserRoleDetail_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
        }

        protected void SelectUserRoleDetail(object sender, DirectEventArgs e)
        {
            objUserRoleDetail = new UserManageRoleDetail();
            objUserRoleDetail.strConn = ConfigurationManager.AppSettings["ConnString"];

            objUserRoleDetail.user_id = Convert.ToInt32(e.ExtraParams["user_id"]);
            DataTable dt = objUserRoleDetail.getByObject(objUserRoleDetail);



            GridPanelUserRoleDetail.Collapsed = false;

            StoreUserRoleDetail.DataSource = dt;
            StoreUserRoleDetail.DataBind();

        }

        protected void AddUserManage(object sender, DirectEventArgs e)
        {
            UserManageAdd1.Show();
        }

        protected void UpdateUser(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["UserValue"];

            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string user_id = string.Empty;
            string user_type_id = string.Empty;
            string username = string.Empty;
            string password = string.Empty;
            string fname_th = string.Empty;
            string lname_th = string.Empty;
            string fname_en = string.Empty;
            string lname_en = string.Empty;


            foreach (Dictionary<string, string> row in workingdays)
            {
                user_id = row["user_id"].ToString();
                user_type_id = row["user_type_name_th"].ToString();
                username = row["username"].ToString();
                password = row["password"].ToString();
                fname_th = row["fname_th"].ToString();
                lname_th = row["lname_th"].ToString();
                fname_en = row["fname_en"].ToString();
                lname_en = row["lname_en"].ToString();

            }

            objUserManage.user_id = Convert.ToInt32(user_id);
            objUserManage.user_type_id = Convert.ToInt32(user_type_id);
            objUserManage.username = username;
            objUserManage.password = password;
            objUserManage.fname_th = fname_th;
            objUserManage.lname_th = lname_th;
            objUserManage.fname_en = fname_en;
            objUserManage.lname_en = lname_en;

            objUserManage.updateData(objUserManage);
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }

        protected void DeleteUser(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteUser();
        }

        protected void AddUserManageRole(object sender, DirectEventArgs e)
        {

            RowSelectionModel sm = this.GridPanelUser.SelectionModel.Primary as RowSelectionModel;
            String user_id = String.Empty;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                user_id = row.RecordID;
            }


            if (user_id == String.Empty)
            {
                MessageWarning("Please Select User!!!");
            }
            else
            {
                UserManageRoleAdd1.Show(user_id);
            }
            
        }

        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteUser()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataUser()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataUser()
        {
            RowSelectionModel sm = this.GridPanelUser.SelectionModel.Primary as RowSelectionModel;
            string user_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                user_id = row.RecordID;
            }

            objUserManage.deleteData(user_id);
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }

        protected void MessageWarning(String message)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Title = "Warning Message",
                Message = message,
                Buttons = MessageBox.Button.OK,
                Icon = (MessageBox.Icon)Enum.Parse(typeof(MessageBox.Icon), "WARNING"),
            });
        }

    }
}